package cn.e4j.modular.api.service;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.api.mapper.Gwat110Mapper;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.crm.entity.GwcmT;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class Gwat110Service{
    
    @Autowired
    Gwat110Mapper gwat110Mapper;

    /**
     * 获取意向商机询单列表
     * GWCMACTI N未提交  Y已提交 X作废
     *
     * @param inDto
     * @return
     */
    public List<Dto> getGwcmData(Page page,Dto inDto) {
        List<Dto> p = new ArrayList<>();
        if(inDto.getString("GWCMACTI").equals("N")){
            p = gwat110Mapper.getGwcmPage(page,inDto);
        }else if(inDto.getString("GWCMACTI").equals("Y") || inDto.getString("GWCMACTI").equals("Z")){
            p = gwat110Mapper.getGwcmPage1(page,inDto);
        }else{
            p = gwat110Mapper.getGwcmPage(page,inDto);
        }
        for (Dto o : p) {
            inDto.put("GWCMDOCNO", o.getString("GWCMDOCNO"));
            o.put("details", gwat110Mapper.getGwcmList(inDto));
        }
        return p;
    }

    /**
     * 获取潜在意向商机询单ID
     *
     * @param inDto
     * @return
     */
    public List<Dto> getStarData(Dto inDto) {
        return gwat110Mapper.getGwcm(inDto);
    }

    /**
     * 更新
     *
     * @param
     */
    @Transactional
    public void update(Dto inDto) {
        GwcmT gwcmT = new GwcmT();
        gwcmT.setGWCMACTI(inDto.getString("GWCMACTI"));
        gwcmT.setGWCMDATE(LocalDate.parse(inDto.getString("GWCMDATE"),DateTimeFormatter.ofPattern(DateUtils.PATTERN_yyyy_MM_dd)));
        // update gwcm_t set gwcmacti = #{gwcmacti}
        // where gwcment = #{gwcment} and gwcmdocno = #{gwcmdocno} and gwcmseq = #{gwcmseq}
        UpdateWrapper<GwcmT> wrapper = new UpdateWrapper<>();
        wrapper.eq("GWCMENT",inDto.getString("GWCMENT"));
        wrapper.eq("GWCMDOCNO",inDto.getString("GWCMDOCNO"));
        wrapper.eq("GWCMSEQ",inDto.getString("GWCMSEQ"));
        gwcmT.update(wrapper);
    }

    /**
     * 获取已审核
     *
     * @param inDto
     * @return
     */
    public List<Dto> getChecked(Dto inDto) {
        return gwat110Mapper.getChecked(inDto);
    }

    /**
     * 获取已购物
     *
     * @param inDto
     * @return
     */
    public List<Dto> getYgw(Dto inDto) {
        return gwat110Mapper.getYgw(inDto);
    }

    /**
     * 获取已下单
     *
     * @param inDto
     * @return
     */
    public List<Dto> getYxd(Dto inDto) {
        return gwat110Mapper.getYxd(inDto);
    }

    /**
     * 获取已发运
     *
     * @param inDto
     * @return
     */
    public List<Dto> getYfy(Dto inDto) {
        return gwat110Mapper.getYfy(inDto);
    }

    /**
     * 获取已安装
     *
     * @param inDto
     * @return
     */
    public List<Dto> getYaz(Dto inDto) {
        return gwat110Mapper.getYaz(inDto);
    }

    /**
     * 获取已评价
     *
     * @param inDto
     * @return
     */
    public List<Dto> getYpj(Dto inDto) {
        return gwat110Mapper.getYpj(inDto);
    }


}
